package com.project.controller.saleOrderController;

import com.github.pagehelper.PageInfo;
import com.project.bean.SaleOrderBean;
import com.project.bean.ShopCarBean;
import com.project.controller.shopController.ShopController;
import com.project.service.GoodsService.IGoodsService;
import com.project.service.saleManagerService.ISaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Map;

@RestController
@RequestMapping("saleOrder")
public class SaleOrderController {
    @Autowired
    private ISaleOrderService service;
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/findByItem")
    public PageInfo<SaleOrderBean> findByItem(int pageNO, String customerName, LocalDate startDate, LocalDate endDate){
        return service.findByItem(pageNO,customerName,startDate,endDate);
    }

    @RequestMapping("/findById")
    public SaleOrderBean findById(int orderId) {
        return service.findById(orderId);
    }

    @RequestMapping("/sale")
    public String addOrder(SaleOrderBean order, HttpServletRequest request) {
        Map<Integer, ShopCarBean> shopMap = (Map<Integer, ShopCarBean>) request.getSession().getAttribute("saleShopMap");
        for(ShopCarBean s : shopMap.values()){
            goodsService.updateNumber(s.getGoods().getId(),s.getGoods().getStockNum()-s.getNum());
        }
        service.addSaleOrder(order, shopMap.values());
        shopMap.clear();
        return "ok";
    }
}
